package com.example.demo.controller;


import com.example.demo.pojo.Page;
import com.example.demo.pojo.Result;
import com.example.demo.pojo.StatusCode;
import com.example.demo.service.ContentService;
import org.apache.tomcat.util.descriptor.web.ContextService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author lee
 * @date 2021年06月14日 19:29
 */
@RestController
public class ContentController {
    @Autowired
    ContentService contentService;



    @GetMapping("/parse/{keyword}")
    public String parse(@PathVariable("keyword")String keyword) throws Exception {
        Boolean flag = contentService.parseContent(keyword);
        if (flag){
            return "操作成功";
        }else {
            return "操作失败";
        }
    }

    @GetMapping("/search/{keyword}/{pageNo}/{pageSize}")
    public Result searchPage(@PathVariable("keyword")String keyword, @PathVariable("pageNo") Integer pageNo, @PathVariable("pageSize") Integer pageSize) throws Exception {
        if (pageNo == null || pageNo == 0 )  {
            pageNo = 1;
        }
        List<Map<String, Object>> maps = contentService.searchPage(keyword, pageNo, pageSize);
        List<Object> temp = new ArrayList<>();
        Page<Object> page = new Page<>();
        //设置数据
        page.setResultList(temp);
        page.getResultList().addAll(maps);


        //设置当前页
        page.setPageNo(pageNo);
        //设置固定页数
        page.setPageSize(pageSize);


        //设置总页数
        page.setTotalCount(contentService.searchTotalCount(keyword));
        return new Result(true, StatusCode.OK,"成功",page);
    }

}
